import Ember from 'ember';
import Changeset from 'ember-changeset';
import AreaValidations from '../../../validations/area';
import lookupValidator from 'ember-changeset-validations';

export default Ember.Controller.extend(AreaValidations,{
  constants:Constants,
  //默认樓宇
  defaultBuilding:Ember.computed('roomInfo.floor.building',function(){
    if(this.get("roomInfo.floor.building")){
      return this.get("roomInfo.floor.building");
    }
  }),
  //默认樓層
  defaultFloor:Ember.computed('roomInfo.floor',function(){
      return this.get('roomInfo.floor');
  }),
  roomModel:Ember.computed("roomInfo",function(){
    var model = this.get("roomInfo");
    if (!model) {
        return null;
    }
    return new Changeset(model, lookupValidator(AreaValidations), AreaValidations);
  }),

  actions:{
    invalid() {
        //alert("error");
    },
    //编辑按钮
    detailEditClick:function(){
      this.set('detailEdit',true);
    },
    //取消按钮
    detailCancel:function(){
      var id=this.get('id');
      var editMode=this.get('editMode');
      if(id&&editMode=='edit'){
        this.set('detailEdit',false);
        this.get('roomInfo').rollbackAttributes();
        var route=App.lookup('route:business.mainpage.public-area-detail');
        App.lookup('controller:business.mainpage').refreshPage(route);//刷新页面
      }else{
        this.set('roomInfo.floor','');
        this.get('roomInfo').rollbackAttributes();
        let mainpageController = App.lookup('controller:business.mainpage');
        mainpageController.switchMainPage('public-area');
      }
    },
    //保存公共区域
    saveArea(){
      var _self=this;
      var editMode=this.get('editMode');
      var roomModel=this.get('roomModel');
        roomModel.validate().then(function(){
          if(roomModel.get('errors.length')===0){
            App.lookup('controller:business.mainpage').openPopTip("正在保存");
            roomModel.set('isPublicFlag',1);
            roomModel.save().then(function(){
              App.lookup('controller:business.mainpage').showPopTip("保存成功");
              if(editMode=='add'){
                var mainpageController = App.lookup('controller:business.mainpage');
                mainpageController.switchMainPage('public-area');
              }else{
                  _self.set('detailEdit',false);
              }
            });
          }else{
            roomModel.set("validFlag",Math.random());
          }
      });
    },


    //删除按钮
    delById : function(room) {
      var _self = this;
      App.lookup('controller:business.mainpage').showConfirm("是否确定删除公共信息",function(){
        _self.send('cancelPassSubmit',_self.get('roomInfo'));
      });
    },
    //选择楼层
    selectFloor(floor) {
        this.set('roomInfo.floor', floor);
        this.set('roomModel.floor',floor);
    },
    //选择楼宇
    selectBuilding(building) {
      var _self = this;
      //this.set('defaultBuilding', building);
      this.store.query('floor',{filter:{building:{id:building.get("id")}}}).then(function(floorList){
        _self.set('floorList',floorList);
        _self.set('floorListFirst',floorList.get("firstObject"));
        // _self.set('defaultBuilding','');
        _self.set('defaultFloor','');
        // console.log("floor is",floorList);
      });
    },
    //弹窗取消
    invitation(){
      this.set('showpopInvitePassModal',false);
    },
    //弹窗确定，删除记录
    cancelPassSubmit(room){
      App.lookup('controller:business.mainpage').openPopTip("正在删除");
			this.set("showpopInvitePassModal",false);
      room.set("delStatus", 1);
      room.save().then(function() {
          App.lookup('controller:business.mainpage').showPopTip("删除成功");
          var mainpageController = App.lookup('controller:business.mainpage');
          mainpageController.switchMainPage('public-area');

      });
		},
  }
});
